﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushBox.Core
{
    public class Person : Position, IMovable
    {
        public bool Move(MoveType moveType)
        {
            switch (moveType)
            {
                case MoveType.Up:
                    Position p = Map.GetObject(X, Y - 1);   //小人问地图：头上有啥
                    if (p is Box)       //地图说：头上有箱子
                    {
                        Box b = (Box)p; // 强制类型转换，因为p是box所以可以转
                        //小人叫箱子向上挪一下
                        if (b.Move(moveType))
                        {
                            Y--;    //如果箱子挪成功了，小人也动一下
                        }
                    }
                    else if (!(p is Wall)) Y--;
                    break;
                case MoveType.Down:
                    Position pd = Map.GetObject(X, Y + 1);
                    if (pd is Box)
                    {
                        Box bd = (Box)pd; // 强制类型转换，因为p是box所以可以转
                        if (bd.Move(moveType))
                        {
                            Y++;
                        }
                    }
                    else if (!(pd is Wall)) Y++;
                    break;
                case MoveType.Left:
                    Position pl = Map.GetObject(X - 1, Y);
                    if (pl is Box)
                    {
                        Box bl = (Box)pl; // 强制类型转换，因为p是box所以可以转
                        if (bl.Move(moveType))
                        {
                            X--;
                        }
                    }
                    else if (!(pl is Wall)) X--;
                    break;
                case MoveType.Right:
                    Position pr = Map.GetObject(X + 1, Y);
                    if (pr is Box)
                    {
                        Box br = (Box)pr; // 强制类型转换，因为p是box所以可以转
                        if (br.Move(moveType))
                        {
                            X++;
                        }
                    }
                    else if (!(pr is Wall)) X++;
                    break;
            }

            this.Map.DoDraw(this.Map);

            return false;
        }
    }
}
